#include "stringutility.h"
#include <string.h>

namespace losor
{
	void strutil::free( const char *str )
	{
		delete []str;
	}
	
	char* strutil::left( const char *src, int len )
	{
		int l = strlen( src );
		char *str = new char[ len + 1 ];
		if( l > len )
			strncpy( str, src, len );
		else
			strcpy( str, src );
		return str;
	}
	
	char* strutil::right( const char *src, int len )
	{
		int l = strlen( src );
		char *str = new char[ len + 1 ];
		if( l > len )
			strcpy( str, src + l - len );
		else
			strcpy( str, src );
		return str;
	}
	
	char* strutil::mid( const char *src, int start, int len )
	{
		int l = strlen( src );
		char *str = new char[ len + 1 ];
		if( l > start+len )
			strncpy( str, src + start, len );
		else if( l > start )
			strcpy( str, src + start );
		else
			strcpy( str, src + l );
		return str;
	}
			
	bool strutil::is_src_ext( const char *name )
	{
		char *ptr;
		ptr = right( name, 4 );
		if( strcmp( ptr, ".cpp" ) == 0 || strcmp( ptr, ".cxx" ) == 0 || strcmp( ptr, ".hpp" ) == 0 )
		{
			strutil::free( ptr );
			return true;
		}
		strutil::free( ptr );
		
		ptr = right( name, 2 );
		if( strcmp( ptr, ".c" ) == 0 || strcmp( ptr, ".h" ) == 0 )
		{
			strutil::free( ptr );
			return true;
		}
		strutil::free( ptr );

		if( strchr( name, '.' ) == NULL )
			return true;
		return false;
	}
}

